package jcode.mybatismapper.app;

import jcode.mybatismapper.dao.UserDao;
import jcode.mybatismapper.model.User;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@EnableAutoConfiguration
@SpringBootApplication
@MapperScan({"jcode.mybatismapper.dao"})
@ComponentScan({"jcode.mybatismapper"})
public class App {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = new SpringApplicationBuilder(App.class).web(true).run(args);
        UserDao userDao = context.getBean(UserDao.class);

        {
            // 插入一条数据
            User user = new User();
            user.setId(1);
            user.setUserName("name1");
            user.setAge(22);
            userDao.insert(user);

            System.out.println(userDao.get(1));
        }

        {
            // 修改数据
            User user2 = userDao.get(1);
            user2.setUserName("newName");
            user2.setAge(11);
            // 只会更新包含@UpdateName注解的字段. 所以只会修改userName属性,不会修改age属性
            userDao.updateByAnnotation(user2, User.UpdateName.class);

            System.out.println(userDao.get(1));
        }

        // 删除数据
        userDao.delete(1);

    }

}